/**
 * 初始化账单详情对话框
 */
var TMonthbalanceDetailInfoDlg = {
    id: "MMTable",	//表格id
    table: null,
    tMonthbalanceDetailInfoData : {}
};

TMonthbalanceDetailInfoDlg.initColumn = function () {
    return [
        {type: 'checkbox', fixed: 'left'},
        {title: '账单号', field: 'balance_num', visible: true, align: 'center', valign: 'middle'},
        {title: '来源', field: 'income', visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
                if(d.income==1){
                    return "收货"
                }else if(d.income==2){
                    return "自提"
                }else if(d.income==3){
                    return "配送"
                }else if(d.income==4){
                    return "中转"
                }else if(d.income==5){
                    return "改单"
                }else if(d.income==6){
                    return "抹平"
                }else if(d.income==7){
                    return "转结"
                }else if(d.income==8){
                    return "新转结"
                }
                return ""

            }},
        {title: '账单状态', field: 'status', visible: true, align: 'center', valign: 'middle',
            templet: function (d) {
                if(d.status==0){
                    return "待结算"
                }
                return "已结算"

            }
        },
        {title: '车次号', field: 'income_name', visible: true, align: 'center', valign: 'middle'},
        {title: '网点/收货人', field: 'traner', visible: true, align: 'center', valign: 'middle'},
        {title: '应收金额', field: 'money', visible: true, align: 'center', valign: 'middle'},
        {title: '账单日期', field: 'createtime', visible: true, align: 'center', valign: 'middle'},
        {title: '制单员', field: 'name', visible: true, align: 'center', valign: 'middle'},
        {title: '备注', field: 'remark', visible: true, align: 'center', valign: 'middle'}

    ];
};

/**
 * 清除数据
 */
TMonthbalanceDetailInfoDlg.clearData = function() {
    this.tMonthbalanceDetailInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TMonthbalanceDetailInfoDlg.set = function(key, val) {
    this.tMonthbalanceDetailInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TMonthbalanceDetailInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TMonthbalanceDetailInfoDlg.close = function() {
    parent.layer.close(window.parent.TMonthbalanceDetail.layerIndex);
}

/**
 * 收集数据
 */
TMonthbalanceDetailInfoDlg.collectData = function() {
    this
    .set('ids')
    .set('cash')
    .set('weixin')
    .set('alipay')
    .set('bank')
    .set('remark')
    .set('m1')
    .set('m2')
    .set('traner')
    .set('income',$('input:radio[name=income]:checked').val())

    ;
}

/**
 * 提交添加
 */
TMonthbalanceDetailInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tMonthbalanceDetail/add", function(data){
        Feng.success("添加成功!");
        window.parent.TMonthbalanceDetail.table.refresh();
        TMonthbalanceDetailInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tMonthbalanceDetailInfoData);
    ajax.start();
}
var mDate=null;
TMonthbalanceDetailInfoDlg.collectSum = function(m) {
    if(m){
        mDate=m;
    }
    var sum=0;
    var balance_num='';
    for(var i=0;i<mDate.data.length;i++){
        sum=sum+parseFloat(mDate.data[i].money);
        balance_num+="["+mDate.data[i].balance_num+"] "
    }
    $('#hj').text(sum);
    $('#balance_num').text(balance_num);
}
/**
 * 提交修改
 */
TMonthbalanceDetailInfoDlg.editSubmit = function() {

    this.clearData();
    var m1=parseFloat($('#hj').text());
    var m2=parseFloat($('#cash').val());
    var m3=parseFloat($('#weixin').val());
    var m4=parseFloat($('#alipay').val());
    var m5=parseFloat($('#bank').val());
    if((m1-(m2+m3+m4+m5)!=0)&&!($('input:radio[name=income]:checked').val()==6||$('input:radio[name=income]:checked').val()==7)){
        Feng.info("应收款项与实际收入不符，请选择差额处理方式[抹平]或者[转结]");
        return;
    }
    if((m1-(m2+m3+m4+m5)==0)&&($('input:radio[name=income]:checked').val()==6||$('input:radio[name=income]:checked').val()==7)){
        Feng.info("应收款项与实际收入相符，请选择差额处理方式[正常]");
        return;
    }
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/tMonthbalanceDetail/update", function(data){
        Feng.success("修改成功!");
        window.parent.aTable.refresh();
        TMonthbalanceDetailInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.tMonthbalanceDetailInfoData);
    ajax.set("hj", $('#hj').text());
    ajax.start();
}

var aTable = null
var mTable = null;
$(function () {
    var defaultColunms = TMonthbalanceDetailInfoDlg.initColumn();
    aTable = new LYTable(TMonthbalanceDetailInfoDlg.id, "/tMonthbalanceDetail/settlementList?ids="+$('#ids').val(), defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        TMonthbalanceDetailInfoDlg.table = aTable.initCallBack(mTable,TMonthbalanceDetailInfoDlg.collectSum);

    })


});
